﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MinGuiLuo.Logging;
using WeifenLuo.WinFormsUI.Docking;

namespace WinFromDevelopFramework
{
    public partial class MainFrm : Form
    {
        public MainFrm( )
        {
            InitializeComponent();
            ClientApp.Register(this);
        }
        ToolboxFrm m_toolbox = null;
        private HomePageFrm m_homePage = new HomePageFrm();
        public void InitFrm( )
        {
            this.dockPanel1.DocumentStyle = DocumentStyle.DockingMdi;
            RightToLeftLayout = true;

            m_toolbox = new ToolboxFrm(this);
            m_toolbox.Show(dockPanel1, DockState.DockLeft);
            m_toolbox.DockTo(this.dockPanel1, DockStyle.Left);

            m_homePage.Show(dockPanel1);

            this.ShowContent("输出", typeof(OutputFrm), DockState.DockBottom);
        }
        #region MyRegion
        private DockContent FindDocument(string text)
        {
            if (dockPanel1.DocumentStyle == DocumentStyle.SystemMdi)
            {
                foreach (Form form in MdiChildren)
                {
                    if (form.Text == text)
                    {
                        return form as DockContent;
                    }
                }
                return null;
            }
            else
            {
                foreach (DockContent content in dockPanel1.Documents)
                {
                    if (content.DockHandler.TabText == text)
                    {
                        return content;
                    }
                }

                return null;
            }
        }
        public DockContent ShowContent(string caption, Type formType, DockState dockState = DockState.Document)
        {
            DockContent frm = FindDocument(caption);
            if (frm == null)
            {
                frm = Activator.CreateInstance(formType) as DockContent;
                if (frm == null)
                {
                    LogUtil.LogDebug(string.Format("业务窗体需要继承WeifenLuo.WinFormsUI.Docking.DockContent窗体，窗体信息：{0} {1}"
                        , caption, formType.ToString()));
                    return null;
                }
                frm.DockHandler.TabText = caption;
                frm.Show(this.dockPanel1, dockState);
            }
            else
            {
                frm.Show(this.dockPanel1, dockState);
                frm.BringToFront();
            }
            return frm;
        }
        #endregion
        private void MainFrm_Load(object sender, EventArgs e)
        {
            InitFrm();
        }
        private void MainFrm_FormClosed(object sender, FormClosedEventArgs e)
        {

        }
        private void 工具箱ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            m_toolbox = new ToolboxFrm(this);
            this.ShowContent("工具箱", m_toolbox.GetType(), DockState.DockLeft);
            m_toolbox.DockTo(this.dockPanel1, DockStyle.Left);
        }
        private void 输出ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.ShowContent("输出", typeof(OutputFrm), DockState.DockBottom);
        }
    }
}
